package com.elive.controller.customer;

import com.elive.common.Const;
import com.elive.common.ServerResponse;
import com.elive.pojo.Order;
import com.elive.pojo.User;
import com.elive.service.IOrderService;
import com.elive.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by wuyin on 2018/4/16.
 */
@Controller
@RequestMapping("/customer/order")
public class OrderController {

    @Autowired
    private IOrderService iOrderService;

    @RequestMapping(value = "getOrderListByRenderId.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getOrderListByRenderId(String sessionId, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        HttpSession httpSession = null;
        if(SessionUtil.sessionMap.containsKey(sessionId)){
            httpSession = SessionUtil.sessionMap.get(sessionId);
        }
        if(httpSession == null){
            return ServerResponse.createByErrorMessage("请先登录");
        }
        User currentUser = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createByErrorMessage("请先登录");
        }
        return iOrderService.getOrderListByRenderId(pageNum,pageSize,currentUser.getId());
    }

    @RequestMapping(value = "setOrder.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse setOrder(String sessionId, Integer apartmentId, Integer hostId, String startTime, String endTime){
        HttpSession httpSession = null;
        if(SessionUtil.sessionMap.containsKey(sessionId)){
            httpSession = SessionUtil.sessionMap.get(sessionId);
        }
        if(httpSession == null){
            return ServerResponse.createByErrorMessage("请先登录");
        }
        User currentUser = (User) httpSession.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createByErrorMessage("请先登录");
        }
        Order dealOrder = new Order();
        dealOrder.setApartmentId(apartmentId);
        dealOrder.setHostId(hostId);
        dealOrder.setRenderId(currentUser.getId());

        return iOrderService.setOrder(dealOrder,startTime,endTime);
    }
}
